﻿using System;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Administration;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using System.ComponentModel;
using Microsoft.SharePoint.Utilities;

namespace CodeCounsel.SharePoint.Flash
{
    public class VideoSettingsPage
        : LayoutsPageBase
    {
        protected CheckBox showIconsField;
        protected CheckBox autoStartField;
        protected DropDownList fullScreenModeField;
        protected TextBox backgroundImageField;

        protected override SPBasePermissions RightsRequired
        {
            get
            {
                return SPBasePermissions.ManageWeb;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            if (IsPostBack == false)
            {
                autoStartField.Checked = VideoSettings.GetWebProperty<bool>(VideoSettings.AutoStart, VideoSettings.AutoStartDefault);
                backgroundImageField.Text = VideoSettings.GetWebProperty<string>(VideoSettings.BackgroundImage, VideoSettings.BackgroundImageDefault);
                showIconsField.Checked = VideoSettings.GetWebProperty<bool>(VideoSettings.ShowIcons, VideoSettings.ShowIconsDefault);
                fullScreenModeField.SelectedValue = VideoSettings.GetWebProperty<string>(VideoSettings.FullScreenMode, VideoSettings.FullScreenModeDefault);
            }
            base.OnLoad(e);
        }

        protected void OkButton_Click(object sender, EventArgs e)
        {
            VideoSettings.SetWebProperty<bool>(VideoSettings.AutoStart, autoStartField.Checked);
            VideoSettings.SetWebProperty<bool>(VideoSettings.ShowIcons, showIconsField.Checked);
            VideoSettings.SetWebProperty<string>(VideoSettings.BackgroundImage, backgroundImageField.Text);
            VideoSettings.SetWebProperty<string>(VideoSettings.FullScreenMode, fullScreenModeField.SelectedValue);
            VideoSettings.Update();
            RedirectToSettingsPage();
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            RedirectToSettingsPage();
        }

        void RedirectToSettingsPage()
        {
            SPUtility.Redirect(
                "settings.aspx", SPRedirectFlags.RelativeToLayoutsPage, Context);
        }
    }
}
